package bh

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.permissionx.guolindev.PermissionX

/**
 * Created by Lingbaeryi
 */

class FV {

    companion object {

        interface PermissionGrant {
            fun onPermissionGranted()
            fun onPermissionCancel()
        }

        fun requestPermissionAct(
            activity: AppCompatActivity,
            permission: ArrayList<String>,
            frontHintMsg: String,
            mPermissionGrant: PermissionGrant
        ) {
            PermissionX.init(activity)
                .permissions(permission)
                .explainReasonBeforeRequest()
                .onExplainRequestReason { scope, deniedList, beforeRequest ->
                    if (beforeRequest) {
                        scope.showRequestReasonDialog(deniedList, frontHintMsg, "我已明白")
                    } else {
                        scope.showRequestReasonDialog(deniedList, "此权限是程序必须依赖的权限", "我已明白")
                    }
                }
                .onForwardToSettings { scope, deniedList ->
                    scope.showForwardToSettingsDialog(
                        deniedList,
                        "您需要去应用程序设置当中手动开启权限",
                        "我已明白",
                        "取消"
                    )
                }
                .request { allGranted, _, deniedList ->
                    if (allGranted) {
                        mPermissionGrant.onPermissionGranted()
                    } else {

                    }
                }
        }


        fun requestPermissionFrg(
            fragment: Fragment,
            permission: ArrayList<String>,
            frontHintMsg: String,
            mPermissionGrant: PermissionGrant
        ) {
            PermissionX.init(fragment)
                .permissions(permission)
//                .explainReasonBeforeRequest()
//                .onExplainRequestReason { scope, deniedList, beforeRequest ->
//                    if (beforeRequest) {
//                        scope.showRequestReasonDialog(deniedList, frontHintMsg, "同意", "取消")
//                    } else {//避免循环请求
//                        scope.showRequestReasonDialog(deniedList, "此权限是程序必须依赖的权限", "同意", "取消")
//                    }
//                }
//                .onForwardToSettings { scope, deniedList ->
//                    scope.showForwardToSettingsDialog(
//                        deniedList,
//                        "您需要去应用程序设置当中手动开启权限",
//                        "去开启",
//                        "取消"
//                    )
//                }
                .request { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        mPermissionGrant.onPermissionGranted()
                    } else {
//                        "您拒绝了如下权限: $deniedList".showToast()
                        mPermissionGrant.onPermissionCancel()
                    }
                }
        }
    }
}